package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.DescBean;
import com.example.netcenter.bean.ErrorBean;
import com.example.netcenter.bean.ResultsData;
import com.example.netcenter.bean.SupplierAccountBean;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.customize.AlertDialog;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

/**
 * Created by lixl on 2018/12/5.
 * <p>
 * desc:保证金入口界面
 * 1 国产，2 进口类
 */
public class MarginActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_margin_back;
    private TextView tv_margin_title;
    private LinearLayout ll_billing_details;

    private TextView tv_total_money;
    private LinearLayout ll_china_margin, ll_import_margin;
    private TextView tv_quoted_money1, tv_refund_money1, tv_quote_available_money1;  //国产类
    private TextView tv_refund_money2, tv_quote_available_money2;  //进口类
    private TextView tv_usage_rules;
    private Button btn_paymargin, btn_refund;
    private TextView tv_notice;

    private String payPwd = "";
    private AlertDialog myDialog;
    private int type = 1;
    private ResultsData<SupplierAccountBean> resultsData;
    private SupplierAccountBean supplierAccountBean;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_margin;
    }

    @Override
    protected void initView() {
        setShowTitle(false);
        setWindowStatusBarColor(MarginActivity.this, R.color.main_color);

        myDialog = new AlertDialog(this).builder();

        type = getIntent().getIntExtra("type", 1);

        iv_margin_back = findViewById(R.id.iv_margin_back);
        tv_margin_title = findViewById(R.id.tv_margin_title);
        ll_billing_details = findViewById(R.id.ll_billing_details);

        tv_total_money = findViewById(R.id.tv_total_money);
        ll_china_margin = findViewById(R.id.ll_china_margin);
        ll_import_margin = findViewById(R.id.ll_import_margin);
        tv_quoted_money1 = findViewById(R.id.tv_quoted_money1);
        tv_refund_money1 = findViewById(R.id.tv_refund_money1);
        tv_quote_available_money1 = findViewById(R.id.tv_quote_available_money1);
        tv_refund_money2 = findViewById(R.id.tv_refund_money2);
        tv_quote_available_money2 = findViewById(R.id.tv_quote_available_money2);

        tv_usage_rules = findViewById(R.id.tv_usage_rules);
        btn_paymargin = findViewById(R.id.btn_paymargin);
        btn_refund = findViewById(R.id.btn_refund);
        tv_notice = findViewById(R.id.tv_notice);

        iv_margin_back.setOnClickListener(this);
        ll_billing_details.setOnClickListener(this);
        btn_paymargin.setOnClickListener(this);
        btn_refund.setOnClickListener(this);
        tv_notice.setOnClickListener(this);

        if (type == 1){
            tv_margin_title.setText("国产类保证金");
            ll_china_margin.setVisibility(View.VISIBLE);
            ll_import_margin.setVisibility(View.GONE);
            tv_usage_rules.setText(getResources().getText(R.string.margin_to_chain));
        } else if (type == 2){
            tv_margin_title.setText("进口类保证金");
            ll_china_margin.setVisibility(View.GONE);
            ll_import_margin.setVisibility(View.VISIBLE);
            tv_usage_rules.setText(getResources().getText(R.string.margin_to_import));
        }
    }

    @Override
    protected void initNetData() {
        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().SUPPLIER_ACCOUNT(ConstantsCode.supplier_account,
                        SharedPreferencesUtil.getID()), new Response() {
                            @Override
                            public void OK(Object data) {
                                Log.e("lixl", data.toString());
                                setView(data);
                            }

                            @Override
                            public void Fail(Throwable e) {
                                Log.e("lixl", e.getMessage());
                                if (e.getMessage().contains("Code")) {
                                    ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                                    showToast(errorBean.getMsg());

                                } else {
                                    showToast(e.getMessage());
                                }
                            }

                            @Override
                            public void complete() {

                            }
                        });

        //条款协议
        // code参数字段：
        // 国产保证金说明 3301#GCBZJ-DESC，
        // 进口保证金说明 3301#JKBZJ-DESC，
        // 供应商协议 3301#GYSXY-DESC
        String code = "";
        if (type == 1){
            code = SharedPreferencesUtil.getWebsiteNode()+"#GCBZJ-DESC";
        } else {
            code = SharedPreferencesUtil.getWebsiteNode()+"#JKBZJ-DESC";
        }
        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().SUPPLIER_TERMS(ConstantsCode.gss_desc, code), new Response() {
            @Override
            public void OK(Object data) {
                ResultsData<DescBean> resultsData = (ResultsData<DescBean>) data;
                DescBean descBean = resultsData.getData();
                tv_usage_rules.setText(descBean.getDesc());
            }

            @Override
            public void Fail(Throwable e) {

            }

            @Override
            public void complete() {

            }
        });
    }

    @Override
    protected void customDestroy() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.iv_margin_back:
                finish();
                break;
            case R.id.ll_billing_details:
                //进入账单明细
                jumpToBilling();
                break;
            case R.id.btn_paymargin:
                //前往去充值
                isFirstPay();
                break;
            case R.id.btn_refund:
                //退款
                refundDialog();
                break;
            case R.id.tv_notice:
                //协议
                jumpToNotice(2);
                break;
        }
    }

    /**
     * 退款提示弹出框
     */
    private void refundDialog(){
        myDialog.setGone()
                .setMsg("是否确认退款，您再考虑考虑！")
                .setNegativeButton("退款", R.color.n_blue, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转到退款界面
                        jumpToRefund();
                    }
                })
                .setPositiveButton("取消", R.color.black, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                .show();
    }

    /**
     * 数据展示
     * @param data
     */
    private void setView(Object data){
        resultsData = (ResultsData<SupplierAccountBean>) data;
        supplierAccountBean = resultsData.getData();
        if (type == 1){
            tv_total_money.setText(supplierAccountBean.getDomesticDeposit()+""); //总金额
        } else if (type == 2){
            tv_total_money.setText(supplierAccountBean.getImportDeposit()+"");
        }
        tv_quoted_money1.setText(supplierAccountBean.getDomesticUsed()+"");  //国产 已报价
        tv_refund_money1.setText(supplierAccountBean.getDomesticRefund()+"");  //国产 退款中
        tv_quote_available_money1.setText(supplierAccountBean.getDomesticCanUse()+""); //国产 可报价

        tv_refund_money2.setText(supplierAccountBean.getImportRefund()+""); //进口 退款中
        tv_quote_available_money2.setText(supplierAccountBean.getImportCanUse()+""); //进口 可报价
        //支付密码
        payPwd = supplierAccountBean.getPassword();
    }

    /**
     * 是否已经支付过了
     * 如果没有支付，跳转条款须知
     */
    private void isFirstPay(){
        GetNetCenter().GetNetCenter(this, RetrofitBuilder.CreateApi().CHECK_FIRST_PAY(
                ConstantsCode.check_first_pay,
                SharedPreferencesUtil.getID(),
                type+""), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());  //{"statusCode":"100000","statusStr":"请求成功","data":false}
                        ResultsData<Boolean> resultsData = (ResultsData<Boolean>) data;
                        Boolean isFirstPay = resultsData.getData();
                        if (isFirstPay){
                            jumpToNotice(1);
                        } else {
                            jumpToPay();
                        }
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                    }

                    @Override
                    public void complete() {

                    }
                });
    }

    /**
     * 跳转 缴纳
     */
    private void jumpToPay(){
        Intent intent = new Intent(MarginActivity.this, MarginToPayActivity.class);
        if (type == 1){
            //国产类
            intent.putExtra("type", 1);  //type，保证金类型
            intent.putExtra("totalMoney", supplierAccountBean.getDomesticDeposit()); //总金额
            intent.putExtra("canUseMoney", supplierAccountBean.getDomesticCanUse());//可报价金额
        } else {
            //进口类
            intent.putExtra("type", 2);  //type，保证金类型
            intent.putExtra("totalMoney", supplierAccountBean.getImportDeposit()); //总金额
            intent.putExtra("canUseMoney", supplierAccountBean.getImportCanUse());//可报价金额
        }
        startActivity(intent);
    }

    /**
     * 跳转 条款
     * @param status
     */
    private void jumpToNotice(int status){
        Intent intent = new Intent(MarginActivity.this, MarginNoticeActivity.class);
        if (status == 1){
            if (type == 1){
                //国产类
                intent.putExtra("type", 1);  //type，保证金类型
                intent.putExtra("totalMoney", supplierAccountBean.getDomesticDeposit()); //总金额
                intent.putExtra("canUseMoney", supplierAccountBean.getDomesticCanUse());//可报价金额
            } else {
                //进口类
                intent.putExtra("type", 2);  //type，保证金类型
                intent.putExtra("totalMoney", supplierAccountBean.getImportDeposit()); //总金额
                intent.putExtra("canUseMoney", supplierAccountBean.getImportCanUse());//可报价金额
            }
        }
        intent.putExtra("status", status);
        startActivity(intent);
    }

    /**
     * 跳转 退款
     */
    private void jumpToRefund(){
        Intent intent = new Intent(MarginActivity.this, RefundActivity.class);
        if (type == 1){
            //国产类
            intent.putExtra("type", 1);  //type，保证金类型
            intent.putExtra("canUseMoney", supplierAccountBean.getDomesticCanUse());//可报价金额
        } else {
            //进口类
            intent.putExtra("type", 2);  //type，保证金类型
            intent.putExtra("canUseMoney", supplierAccountBean.getImportCanUse());//可报价金额
        }
        intent.putExtra("payPwd", payPwd);
        startActivity(intent);
    }

    /**
     * 跳转 账单
     */
    private void jumpToBilling(){
        Intent intent = new Intent(MarginActivity.this, BillingListActivity.class);
        intent.putExtra("type", type);
        startActivity(intent);
    }
}
